package com.daidai.activitys

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Handler
import android.os.Message
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.daidai.base.BaseActivity
import com.daidai.spinner.R
import com.daidai.tools.G
import java.util.*

object AppConfig{
    //-------------------------
    // 通知channel
    const val Channel_Main_Id = "id_resident"
    const val Channel_Main_Name = "常驻通知"
    const val Nid_Main = 0x111    // 通知Id

    const val Channel_Remind_Id = "id_remind"  // 各种提醒
    const val Channel_Remind_Name = "提醒通知"
    const val Nid_Remind = 0x1  // 签到 吃饭提醒 步数完成
}

class RemoteViewActivity : BaseActivity() {

    var timer : Timer? = null
    var timerTask = object : TimerTask() {
        override fun run() {
            handler.sendEmptyMessage(1)
        }
    }
    val handler = object:Handler(){
        override fun handleMessage(msg: Message) {
            if(msg.what == 1){
                updateRemoteView()
            }
        }
    }

    var bs = 0

    override fun getContentViewResId() = R.layout.activity_remoteview

    override fun initView() {
        findBtn(R.id.btn1).setOnClickListener {
            val remoteView = RemoteViews(packageName,R.layout.remote_view)
            sendCustomeView("btn1","第一个通知",remoteView)
        }
        findBtn(R.id.btn2).setOnClickListener {
            sendRemindNotify("btn2","第2个通知")
        }
        timer = Timer()
        timer!!.schedule(timerTask,2000L,2000L)
    }


    override fun onDestroy() {
        timer?.cancel()
        timer = null
        super.onDestroy()
    }

    override fun initData() {
    }

    // 发送各种提醒通知
    private fun sendRemindNotify(
            title:String,
            desc:String
    ){
        // 这里要用 applicationContext.... 真是尼玛坑B
        // 启动前台服务
        val manager = NotificationManagerCompat.from(applicationContext)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(AppConfig.Channel_Remind_Id, AppConfig.Channel_Remind_Name, NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }

        val mainIntent = Intent(applicationContext, RemoteViewActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(applicationContext, 1, mainIntent, PendingIntent.FLAG_ONE_SHOT)

        val notification: Notification = NotificationCompat.Builder(this, AppConfig.Channel_Remind_Id)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)  // 优先级
                .setContentTitle(title)
                .setContentText(desc)
                .setContentIntent(pendingIntent)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)  // 设置锁定屏幕公开范围
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))
                .setAutoCancel(true).build()
        manager.notify(AppConfig.Nid_Remind, notification)
    }

    // 发送各种提醒通知
    private fun sendCustomeView(
        title:String,
        desc:String,
        view:RemoteViews
    ){

        // 这里要用 applicationContext.... 真是尼玛坑B
        // 启动前台服务
        val manager = NotificationManagerCompat.from(applicationContext)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(AppConfig.Channel_Remind_Id, AppConfig.Channel_Remind_Name, NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }

        val mainIntent = Intent(applicationContext, RemoteViewActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(applicationContext, 1, mainIntent, PendingIntent.FLAG_ONE_SHOT)

        val notification: Notification = NotificationCompat.Builder(this, AppConfig.Channel_Remind_Id)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)  // 优先级
            .setContentTitle(title)
            .setContentText(desc)
            .setOngoing(true) //设置true表示是一个正在活动的后台任务，不会被清除掉，比如音乐播放器
            .setCustomContentView(view)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)  // 设置锁定屏幕公开范围
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))
            .setAutoCancel(false).build()
        manager.notify(AppConfig.Nid_Remind, notification)
    }

    fun updateRemoteView(){
        bs++
        val remoteView = RemoteViews(packageName,R.layout.remote_view)
        remoteView.let {
            G.elog("updateRemoteView called!")
            it.setImageViewResource(R.id.ivPhone,R.mipmap.phone)
            it.setImageViewResource(R.id.ivMoney,R.mipmap.money)
            it.setTextViewText(R.id.tvBuShu,"$bs")
        }
        sendCustomeView("111","222",remoteView)
    }
}